package GUI;

import Socket.TCPClient;

import com.sun.image.codec.jpeg.*;

import java.awt.BorderLayout;

import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import net.miginfocom.swing.MigLayout;
import javax.swing.JPasswordField;
import javax.swing.JButton;

public class LogInWithSiteKey extends JFrame {

	private JPanel contentPane;
	private JPasswordField passwordField;
	private String userID;

	/**
	 * Launch the application.
	 */
	public void fire(String s) {
		userID = s;
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LogInWithSiteKey frame = new LogInWithSiteKey();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @throws IOException 
	 */
	public LogInWithSiteKey() throws IOException {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new MigLayout("", "[115.00,center][149.00,grow,center][grow,center]", "[43.00][49.00][50.00][52.00]"));
		
		JLabel lblSecureAucitionMarketplace = new JLabel("Secure Aucition Marketplace");
		contentPane.add(lblSecureAucitionMarketplace, "cell 0 0 3 1,alignx center");
		
		JLabel lblUserId = new JLabel("User ID:");
		contentPane.add(lblUserId, "cell 0 1,alignx center");
		
		JLabel lblNAiQiao = new JLabel(userID);
		contentPane.add(lblNAiQiao, "cell 1 1");
		
		JLabel lblSiteKey = new JLabel("Site Key:");
		contentPane.add(lblSiteKey, "cell 0 2,alignx center");
		
		BufferedImage myPicture = ImageIO.read(new File("./SiteKeys/getSiteKey_2.jpeg"));
		JLabel lblNewLabel = new JLabel(new ImageIcon(myPicture));
		contentPane.add(lblNewLabel, "cell 1 2");
		
		
		JLabel lblPassword = new JLabel("Password:");
		contentPane.add(lblPassword, "cell 0 3,alignx center");
		
		passwordField = new JPasswordField();
		contentPane.add(passwordField, "cell 1 3,growx");
		
		JButton btnLogOn = new JButton("Log On");
		contentPane.add(btnLogOn, "cell 2 3,alignx center");
		btnLogOn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				TCPClient.sendData(String.valueOf(passwordField.getPassword()));
				dispose();
				Hall hall = new Hall();
				Hall.fire();
			}
		});
		
	}

}
